package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ILawsRegulationsDAO;
import org.jsola.hr.dto.LawsRegulationsAddDTO;
import org.jsola.hr.dto.LawsRegulationsUpdateDTO;
import org.jsola.hr.entity.LawsRegulationsDO;
import org.jsola.hr.query.LawsRegulationsQuery;
import org.jsola.hr.service.ILawsRegulationsService;
import org.jsola.hr.vo.LawsRegulationsListVO;
import org.jsola.hr.vo.LawsRegulationsVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 卫人云-政策法规
 *
 * @author zhr
 */
@Service("hrLawsRegulationsServiceImpl")
public class LawsRegulationsServiceImpl implements ILawsRegulationsService {

    @Autowired
    private ILawsRegulationsDAO lawsRegulationsDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public LawsRegulationsVO save(LawsRegulationsAddDTO lawsRegulationsAddDTO, TokenUser tokenUser) {
        // 转DO
        LawsRegulationsDO lawsRegulationsDO = lawsRegulationsAddDTO.to(LawsRegulationsDO.class);
        // 保存
        lawsRegulationsDO = save(lawsRegulationsDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return lawsRegulationsDO.to(LawsRegulationsVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(LawsRegulationsUpdateDTO lawsRegulationsUpdateDTO, TokenUser tokenUser) {
        //转DO
        LawsRegulationsDO lawsRegulationsDO = lawsRegulationsUpdateDTO.to(LawsRegulationsDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(lawsRegulationsDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... lawsRegulationsIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) lawsRegulationsIds);
    }


    @Override
    public LawsRegulationsVO selectById(String lawsRegulationsId, String siteId) {
        LawsRegulationsDO lawsRegulationsDO = selectDOById(lawsRegulationsId, siteId);
        if (lawsRegulationsDO == null) {
            return null;
        }
        return lawsRegulationsDO.to(LawsRegulationsVO.class);
    }

    @Override
    public List<LawsRegulationsListVO> select(LawsRegulationsQuery lawsRegulationsQuery, String siteId) {
        List<LawsRegulationsDO> lawsRegulationsDOList = selectDO(lawsRegulationsQuery, siteId);
        if(CollectionUtils.isEmpty(lawsRegulationsDOList)) {
            return lawsRegulationsDOList == null ? null : new ArrayList<>();
        }
        return lawsRegulationsDOList.stream()
                .map(lawsRegulationsDO -> lawsRegulationsDO.to(LawsRegulationsListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(LawsRegulationsQuery lawsRegulationsQuery, String siteId) {
        Example example = buildExample(lawsRegulationsQuery,siteId);
        return lawsRegulationsDAO.selectCountByExample(example);
    }

    @Override
    public Page<LawsRegulationsListVO> selectPage(LawsRegulationsQuery lawsRegulationsQuery, String siteId) {
        Example example = buildExample(lawsRegulationsQuery,siteId);
        Page<LawsRegulationsDO> page = lawsRegulationsDAO.selectPageByExample(example,
                lawsRegulationsQuery.getPageNo(),
                lawsRegulationsQuery.getPageSize());

        return page.to(LawsRegulationsListVO.class);
    }

    @Override
    public LawsRegulationsDO selectDOById(String lawsRegulationsId, String siteId) {
        return listById(lawsRegulationsId, siteId);
    }

    @Override
    public List<LawsRegulationsDO> selectDO(LawsRegulationsQuery lawsRegulationsQuery, String siteId) {
        Example example = buildExample(lawsRegulationsQuery,siteId);
        return lawsRegulationsDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param lawsRegulationsQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(LawsRegulationsQuery lawsRegulationsQuery, String siteId) {
        Example example = new Example(LawsRegulationsDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,lawsRegulationsQuery.getOrders());
        return example;
    }
}




